package com.hunray.rent.controller;

import java.util.HashSet;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hunray.rent.entity.Permission;
import com.hunray.rent.entity.Resource;
import com.hunray.rent.entity.Role;
import com.hunray.rent.entity.User;

@Controller
public class IndexController {
	@RequestMapping(value = { "/index", "/" })
	public ModelAndView index(User user) {
		Set<Role> roles = new HashSet<Role>(user.getRoles());
		Set<Permission> permissions = new HashSet<>();
		for (Role r : roles) {
			permissions.addAll(r.getPermissions());
		}
		Set<Resource> resources = new HashSet<>();
		for (Permission p : permissions) {
			resources.add(p.getResource());
		}
		ModelAndView mv = new ModelAndView();
		mv.addObject("menus", resources);
		mv.setViewName("index");
		return mv;
	}

	@RequestMapping(value = "/unauthorized")
	public String unauthorized() {
		return "unauthorized";
	}
}
